package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.domain.entity.SysUser;
import com.hospital.common.core.domain.model.LoginUser;
import com.hospital.common.helper.LoginHelper;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hospital.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.TemplateInfoBo;
import com.hospital.workstation.domain.vo.TemplateInfoVo;
import com.hospital.workstation.domain.TemplateInfo;
import com.hospital.workstation.mapper.TemplateInfoMapper;
import com.hospital.workstation.service.ITemplateInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工作站模板信息Service业务层处理
 *
 * @author whb
 * @date 2023-02-10
 */
@RequiredArgsConstructor
@Service
public class TemplateInfoServiceImpl implements ITemplateInfoService {

    private final TemplateInfoMapper baseMapper;
    private final SysUserMapper userMapper;

    /**
     * 查询工作站模板信息
     */
    @Override
    public TemplateInfoVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工作站模板信息列表
     */
    @Override
    public TableDataInfo<TemplateInfoVo> queryPageList(TemplateInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TemplateInfo> lqw = buildQueryWrapper(bo);
        Page<TemplateInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工作站模板信息列表
     */
    @Override
    public List<TemplateInfoVo> queryList(TemplateInfoBo bo) {
        LambdaQueryWrapper<TemplateInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TemplateInfo> buildQueryWrapper(TemplateInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TemplateInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTemplateName()), TemplateInfo::getTemplateName, bo.getTemplateName());
        lqw.eq(StringUtils.isNotBlank(bo.getTemplateContent()), TemplateInfo::getTemplateContent, bo.getTemplateContent());
        lqw.eq(StringUtils.isNotBlank(bo.getTemplateType()), TemplateInfo::getTemplateType, bo.getTemplateType());
        return lqw;
    }

    /**
     * 新增工作站模板信息
     */
    @Override
    public R<Void> insertByBo(TemplateInfoBo bo) {
        // 判断登录状态
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (ObjectUtil.isNull(loginUser)) {
            return R.fail("未登录,请您登录");
        }
        // 业务类转为实体类
        TemplateInfo add = BeanUtil.toBean(bo, TemplateInfo.class);
        validEntityBeforeSave(add);
        // 判断模板类型是否为空
        if (StringUtils.isBlank(bo.getTemplateType())) {
            return R.fail("模板类型不能为空");
        }
        // 设置创建人姓名
        SysUser sysUser = userMapper.selectUserById(loginUser.getUserId());
        add.setCreateBy(sysUser.getNickName());
        // 新增
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return R.ok("新增成功");
    }

    /**
     * 修改工作站模板信息
     */
    @Override
    public Boolean updateByBo(TemplateInfoBo bo) {
        TemplateInfo update = BeanUtil.toBean(bo, TemplateInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TemplateInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工作站模板信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
